/*
 * This class is used to get data about configuration page
 */

package com.fipl.parse;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;

public class ParseConfigDetail {

	private static String URL1 = "http://megan.teksar.com.mx/json.php?dbname=" ;
	private static String URL2 = "&action=select&table=configdetail&where=%20and%20";
	private String args="";
	private StringBuffer jsonText = new StringBuffer();
	public Parser parseConfig;										// create parser class object
	private ArrayList<String> id_configdetailArrayList = new ArrayList<String>();	// store value of id_config in to array list
	private ArrayList<String> encabezadoArrayList = new ArrayList<String>();		// store value of encabezado in to array list
	private ArrayList<String> parametroArrayList = new ArrayList<String>();			// store value of parametro in to array list
	private ArrayList<String> valorArrayList = new ArrayList<String>();				// store value of valor in to array list

	public ParseConfigDetail(String dbName,String args) {
		// TODO Auto-generated constructor stub
		this.args = args;
		parseConfig = new Parser();
		try{
			// concatenating url to form complete url.
			String urlStr = (URL1+dbName+URL2+this.args).replace(" ", "%20");
			URL urlDetail=new URL(urlStr);											// create url object to get data from given url
			HttpURLConnection httpConn=(HttpURLConnection)urlDetail.openConnection();	// open url connection to the given url
			InputStreamReader inDetail = new InputStreamReader(httpConn.getInputStream());	// get input stream from http connection and read
			BufferedReader bDetail = new BufferedReader(inDetail);							
			String lineBuffer="";
			do{
				lineBuffer=bDetail.readLine();									// read bufferedreader line by line
				jsonText.append(lineBuffer);									// append line by line data to the string buffer
			}while(lineBuffer!=null);
			httpConn.disconnect();												// disconnect http connection from given url
			bDetail.close();													//  close buffer reader
		} 
		catch (Exception e) {
			Log.i("Exception ParserConfig : ", e.toString());
		}
	}
	
	public int ParseData(int recordNo){
		int parseReturn =0;
		JSONArray jsonArray;
		try{
			jsonArray=new JSONArray(jsonText.toString());
			for(int i=recordNo; i<jsonArray.length();i++){
				JSONObject jsonObject=jsonArray.getJSONObject(i);
				id_configdetailArrayList.add(jsonObject.getString("ID_configdetail"));				// get and store data of ID_Configuration in the id_configdetail array list.
				encabezadoArrayList.add(jsonObject.getString("encabezado"));						// get and store data of encabezado in the encabezado array list.
				parametroArrayList.add(jsonObject.getString("parametro"));							// get and store data of parametro in the parametro array list.
				valorArrayList.add(jsonObject.getString("valor"));									//get and store data of valor in the valor array list.
			}
			// set array list value in the respectively
			parseConfig.setId_configdetailArrayList(id_configdetailArrayList);						
			parseConfig.setEncabezadoArrayList(encabezadoArrayList);
			parseConfig.setParametroArrayList(parametroArrayList);
			parseConfig.setValorArrayList(valorArrayList);

		}catch (Exception e) {
			// TODO: handle exception
			// return -1 if any exception is thrown
			parseReturn = -1;					
		}
		return parseReturn;
	}

}
